package org.shan.hangtian.ui;

import io.netty.channel.Channel;
import org.shan.hangtian.server.session.SessionManager;
import org.shan.hangtian.tp.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class CenterPanel extends JLabel {


    private JButton modifyMode;

    private Color mode1_color = new Color(40, 40, 40);

    private Color mode2_color = new Color(220, 220, 220);

    private JLabel center, currMusic;

    private RunMode currMode;

    public static File dir = new File("");

    public CenterPanel(RunMode runMode, JLabel center, boolean connected) {
        this.center = center;
        this.currMode = runMode;
        Color fontColor;
        BaseMusic[] baseMusic;
        if (RunMode.MODE_1.equals(runMode)) {
            fontColor = mode1_color;
            baseMusic = Mode1Music.values();
        } else if (RunMode.MODE_2.equals(runMode)) {
            fontColor = mode2_color;
            baseMusic = Mode2Music.values();
        } else {
            fontColor = mode1_color;
            baseMusic = new BaseMusic[]{};
        }

        setLayout(new BorderLayout());
        setBorder(new EmptyBorder(0, 50, 20, 50));
        setBackground(null);
        setOpaque(false);
        {
            JPanel panel = new JPanel(new GridLayout(2, 1));
            panel.setPreferredSize(new Dimension(0, 110));
            panel.setBackground(null);
            panel.setOpaque(false);
            {
                JPanel mode = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 30));
                mode.setBorder(new MatteBorder(0, 0, 0, 0, new Color(220, 220, 220)));
                mode.setBackground(null);
                mode.setOpaque(false);
                {
                    JLabel label = new JLabel("正在播放：");
                    label.setFont(new Font("黑体", Font.PLAIN, 20));
                    label.setForeground(fontColor);
                    mode.add(label);

                    if (connected)
                        currMusic = new JLabel(baseMusic.length > 0 ? baseMusic[0].getName() : "");
                    else
                        currMusic = new JLabel("");
                    currMusic.setFont(new Font("黑体", Font.PLAIN, 20));
                    currMusic.setForeground(fontColor);
                    mode.add(currMusic);
                }
                panel.add(mode);

                mode = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 10));
                mode.setBorder(new MatteBorder(0, 0, 1, 0, new Color(220, 220, 220)));
                mode.setBackground(null);
                mode.setOpaque(false);
                {
                    JLabel label = new JLabel("当前模式：");
                    label.setFont(new Font("黑体", Font.PLAIN, 20));
                    label.setForeground(fontColor);
                    mode.add(label);

                    JLabel currMode = new JLabel(runMode.toString());
                    currMode.setFont(new Font("黑体", Font.PLAIN, 20));
                    currMode.setForeground(fontColor);
                    mode.add(currMode);

                    modifyMode = new JButton("更改模式");
                    modifyMode.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                    modifyMode.setBackground(new Color(69, 142, 253));
                    modifyMode.setForeground(new Color(255, 255, 255));
                    modifyMode.setFont(new Font("黑体", Font.PLAIN, 16));
                    modifyMode.setFocusPainted(false);
                    mode.add(modifyMode);
                }
                panel.add(mode);
            }
            add(panel, BorderLayout.NORTH);

            JPanel songs = new JPanel(new BorderLayout());
            songs.setBackground(null);
            songs.setOpaque(false);
            {
                panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 30));
                panel.setPreferredSize(new Dimension(0, 90));
                panel.setBackground(null);
                panel.setOpaque(false);
                {
                    JLabel label = new JLabel("歌曲列表：");
                    label.setFont(new Font("黑体", Font.PLAIN, 20));
                    label.setForeground(fontColor);
                    panel.add(label);
                }
                songs.add(panel, BorderLayout.NORTH);

                panel = new JPanel(new GridLayout(1, 1));
                panel.setBorder(new EmptyBorder(0, 20, 0, 0));
                panel.setBackground(null);
                panel.setOpaque(false);
                {
                    JScrollPane scrollPane = new JScrollPane();
                    scrollPane.setBorder(null);
                    scrollPane.setBackground(null);
                    scrollPane.setOpaque(false);
                    scrollPane.getViewport().setOpaque(false);
                    {
                        JPanel songList = new JPanel(new VFlowLayout(FlowLayout.LEFT, 0, 10, true, false));
                        songList.setBackground(null);
                        songList.setOpaque(false);
                        {
                            for (BaseMusic music : baseMusic) {
                                MusicPanel musicPanel = new MusicPanel(currMode, music, fontColor, CenterPanel.this);
                                songList.add(musicPanel);
                                musicPanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                            }

                        }
                        scrollPane.setViewportView(songList);
                    }
                    panel.add(scrollPane);
                }
                songs.add(panel, BorderLayout.CENTER);
            }
            add(songs, BorderLayout.CENTER);

            panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 0));
            panel.setBackground(null);
            panel.setOpaque(false);
            {
                JLabel label = new JLabel();
                label.setOpaque(true);
                label.setIcon(new ImageIcon(ClassLoader.getSystemResource("imgs/1.jpg")));
                panel.add(label);

                label = new JLabel();
                label.setOpaque(true);
                label.setIcon(new ImageIcon(ClassLoader.getSystemResource("imgs/2.jpg")));
                panel.add(label);
            }
            add(panel, BorderLayout.SOUTH);
        }

        addListeners();
    }

    private void addListeners() {
        modifyMode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Channel onlineChannel = SessionManager.getInstance().getOnlineChannel();
                if (onlineChannel == null) {
                    JOptionPane.showMessageDialog(CenterPanel.this, "未连接设备");
                    return;
                }

                ModeDialog modeDialog = new ModeDialog(CenterPanel.this);
                modeDialog.setVisible(true);
            }
        });

    }

    public void changeMusic(BaseMusic music) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                currMusic.setText(music.getName());
                currMusic.updateUI();
            }
        });
    }

    public void changeMode(RunMode mode) {
        Channel onlineChannel = SessionManager.getInstance().getOnlineChannel();
        if (onlineChannel == null) {
            JOptionPane.showMessageDialog(CenterPanel.this, "未连接设备");
            return;
        }
        currMode = mode;
        CenterPanel centerPanel = new CenterPanel(mode, center, true);
        ImageIcon imageIcon;
        if (RunMode.MODE_1.equals(mode)) {
            imageIcon = new ImageIcon(ClassLoader.getSystemResource("imgs/mode1.jpg"));
            onlineChannel.writeAndFlush(HangtianPacket.changeMusic(RunMode.MODE_1, (byte) 0));
        } else if (RunMode.MODE_2.equals(mode)) {
            imageIcon = new ImageIcon(ClassLoader.getSystemResource("imgs/mode2.jpg"));
            onlineChannel.writeAndFlush(HangtianPacket.changeMusic(RunMode.MODE_2, (byte) 0));
        } else {
            imageIcon = new ImageIcon(ClassLoader.getSystemResource("imgs/mode3.jpg"));
            onlineChannel.writeAndFlush(HangtianPacket.changeMusic(RunMode.MODE_OTHER, (byte) 0));
        }

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                center.setIcon(imageIcon);
                center.removeAll();
                center.add(centerPanel);

                center.updateUI();
            }
        });
    }
}
